<?php

namespace common\observer;

use support\SingletonTrait;
use common\service\MessageService;
use teamones\dbeventObserver\interfaces\SplSubject;
use teamones\dbeventObserver\interfaces\SplObserver;

/**
 * 任务状态变化 通知执行人
 */
class TaskStatusChangeNotifyExecutor implements SplObserver
{
    use SingletonTrait;

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        if (!isset($updateData['task_status'])) {
            return;
        }
        $task = $subject->getSubjectData();

        if (empty($task['executor'])) {
            return;
        }
        try {
            MessageService::getInstance()->notifyUserTaskStatusChanged($task['executor'], $subject->getSplPrimaryId());
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
